var Goods = function() {

	var left_margin_step = 15;
	$("#qrygoods").bind(
			'click',
			function() {
				var myts = new Date();
				//var formStr = $('#prod_filter_form').serialize();
				var formStr = $(this).parents("form").find("#prod_filter").serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context + "/other/qryGoodsClass?myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						if(result != "[]"){
							var orgArr = eval(result);
							$("#productPicker").find(".tree_list2").empty();
							for ( var loop = 0; loop < orgArr.length; loop++) {
								var obj = orgArr[loop];
								var a = $("<a></a>").attr("href", "#")
										.addClass("category_trigger2")
										.attr("margin", 0);								
								var inputObj = $("<input/>").val(obj.pk_marbasclass)
												.attr("type", "hidden")
												.attr("name", "pk_marbasclass_intree");	
										
								var label = $("<label></label>").text(obj.name);
								var div = $("<div></div>")
										.addClass("tree_category2").append(a)
										.append(inputObj).append(label);												
								$("#productPicker").find(".tree_list2").append(div);
							}
						}
					}				
				})
				var position = $(this).offset();
				position.top += 25;
				$("#productPicker").slideDown("normal");
				$("#productPicker").offset(position);
			});

	$("#filterProd").bind(
			'click',
			function() {
				var myts = new Date();
				//var formStr = $('#prod_filter_form').serialize();
				var formStr = $(this).parents("form").find("#prod_filter").serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context + "/other/filterGoodsClass?myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						if(result != "[]"){
							var orgArr = eval(result);
							$("#productPicker").find(".tree_list2").empty();
							for ( var loop = 0; loop < orgArr.length; loop++) {
								var obj = orgArr[loop];
								var a = $("<a></a>").attr("href", "#")
										.addClass("category_trigger2")
										.attr("margin", 0);								
								var inputObj = $("<input/>").val(obj.pk_marbasclass)
												.attr("type", "hidden")
												.attr("name", "pk_marbasclass_intree");	
								var label = $("<label></label>").text(obj.name);
								var div = $("<div></div>")
										.addClass("tree_category2").append(a)
										.append(inputObj).append(label);												
								$("#productPicker").find(".tree_list2").append(div);
							}
						}
					}				
				})
			});
			
//	$("#qrygoods").UIDropDialog( {
//		'triggerClass' : "idTreePicker",
//		'content' : $("#productPicker")
//	});

	//"close"样式(-)是已经展开时的样式
	// 控制方法：
	// 如果当前节点尚未展开(+)，此次点击触发下列行为：(1)将当前节点样式设置为已展开；(2)从后台查数据，渲染子节点;(3)将同辈的其他已展开节点及其子节点收缩，且将收缩节点的样式设置为收缩样式
	// 如果当前节点已经展开(-)，此次点击触发下列行为：(1)将当前节点的子节点收缩；(2)当前节点的样式设置为展开(-)
	$(".category_trigger2").live(
			'click',
			function() {
				var category_triggerObj=$(this);
				if(category_triggerObj.hasClass("close")){
					var nextChild = category_triggerObj.parent(".tree_category2").next(".children");
					nextChild.remove();
					category_triggerObj.removeClass("close");
					return false;
				}
				$(this).addClass("close");
				//var pk_org_val = $(this).next("input").val();
				var pk_marbasclass = $(this).next("input").val();
				var myts = new Date();
				//var formStr = $('#prod_filter_form').serialize();
				var formStr = $(this).parents("form").find("#prod_filter").serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context
							+ "/other/qryGoodsClassByPk?pk_marbasclass="
							+ pk_marbasclass  
							+ "&myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						var orgArr = eval(result);
						var childrenDiv = $("<div></div>").addClass("children");
						var current_margin_left_size = parseInt(category_triggerObj.attr("margin")) + left_margin_step;
						if(orgArr.length!=0){
							for ( var loop = 0; loop < orgArr.length; loop++) {
								var obj = orgArr[loop];
								var a = $("<a></a>").attr("href", "#").addClass("category_trigger2")
										.attr("style","margin-left:"+current_margin_left_size+"px")
										.attr("margin", current_margin_left_size);
								var inputObj = $("<input/>").val(obj.pk_marbasclass)
												.attr("type","hidden")
												.attr("name", "pk_marbasclass_intree");
								var label = $("<label></label>").text(obj.name);
								var div = $("<div></div>")
										.addClass("tree_category2").append(a)
										.append(inputObj).append(label);
								childrenDiv.append(div);
							}
							childrenDiv.insertAfter(category_triggerObj.parent());
						}else{
							jQuery.ajax({
								type : "POST",
								async : false,
								url : ebvp_context
										+ "/other/qryGoodsByPk?pk_marbasclass="
										+ pk_marbasclass  
										+ "&myts=" + myts,
								dataType : "text",
								data : "&" + formStr,
								success : function(result) {
									var GoodsArr=eval(result);
							       	for ( var loop = 0; loop < GoodsArr.length; loop++) {
										var obj = GoodsArr[loop];
										var prodPk = obj.pk_source;
										var prodName = obj.name;
										var inputObj = $("<input/>")
														.attr("type", "checkbox")
														.attr("id", "pk_material_intree")
														.attr("name", "pk_material_intree")
														//.attr("click", "abc(111)")
														.val(obj.pk_source+"_"+obj.name)
														.addClass(obj.pk_source)
														.attr("style","margin-left:"+current_margin_left_size+"px;margin-top:4px");
										$(inputObj).click(function(){
											var checkValue = $(this).val();
											var checkProdPk = checkValue.substr(0,20);
											var checkProdName = checkValue.substr(21);
											if(this.checked){
												var tempLength=0;
												var deleteProdFlag = 0;
												var checkBox = $("<input/>")
																.attr("type", "checkbox")
	//																.attr("click", "removeSelProd(this)")
																.val(checkValue)
																.attr("style","margin-left:0px;px;margin-top:0px").attr("checked","checked");
												$(checkBox).click(function(){
													var checkValue = $(this).val();
													var checkProdPk = checkValue.substr(0,20);
													var checkProdName = checkValue.substr(21);
													var objs = $("."+checkProdPk);
													if(objs.length > 0){
														objs[0].checked = false;
													}
													$(this).parent().remove();
													var divHeight = $("#sel_prod")[0].offsetHeight;
													var allChild = $("#sel_prod").children();
													var lastChildHeight = allChild[allChild.length - 1].offsetTop;																							
													//设置已选产品去高度
													var objDialogs = $(".ui-drop_dialog_content");
													var idxObj;
													for(idxObj=0;idxObj<objDialogs.length;idxObj++){
														var objDialog = $(objDialogs[idxObj]);
														if(objDialog.is(":visible")){
															objDialog.attr("style","height:"+ lastChildHeight +"px");
															break;
														}
													}
													$($(".border.lft")[idxObj]).attr("style","height:"+ lastChildHeight +"px");
													$($(".border.rit")[idxObj]).attr("style","height:"+ lastChildHeight +"px");
													$(".ui-drop_dialog_content").attr("style","height:"+ lastChildHeight +"px");
													$(".border.lft").attr("style","height:"+ lastChildHeight +"px");
													$(".border.rit").attr("style","height:"+ lastChildHeight +"px");
													$("#sel_prod").attr("style","height:"+ lastChildHeight +"px");
												});
												var labelProd = $("<label/>").html(checkProdName);
												var div = $("<div></div>").attr("id", checkProdPk).attr("name", checkProdPk).attr("style", "float:left;display:inline").append(checkBox).append(labelProd);
												$("#sel_prod").append(div);;
											}else{											
												$("#"+checkProdPk).remove();
											}
											var allChild = $("#sel_prod").children();
											var lastChildHeight = allChild[allChild.length - 1].offsetTop;											
											$(".ui-drop_dialog_content").attr("style","height:"+ lastChildHeight +"px");
							                $(".border.lft").attr("style","height:"+ lastChildHeight +"px");
							                $(".border.rit").attr("style","height:"+ lastChildHeight +"px");
							                $("#sel_prod").attr("style","height:"+ lastChildHeight +"px");																				

										});
										
										//查询该物料是否已经被选择
										var objProd = $("#"+obj.pk_source);
										if(objProd.length > 0){
											inputObj.attr("checked", "checked");
										}
										var label = $("<label></label>").text(obj.name + " " + obj.materialspec  + " " + obj.materialtype);
										var inputObj1 = $("<input/>").val(obj.pk_source).attr(
												"type", "hidden");
										var div = $("<div></div>")
												.addClass("tree_category2").append(inputObj).append(label).append(inputObj1);
										childrenDiv.append(div);
						        	}
							  		childrenDiv.insertAfter(category_triggerObj.parent());
						    	}
							})
						}
					}
				});
				
				//自身展开后，将其兄弟节点及其子节点收缩回
				 category_triggerObj.parent(".tree_category2").siblings(".tree_category2").next(".children").slideUp(function(){
					 $(this).prev(".tree_category2").find(".category_trigger2").removeClass("close");
				 });

				return false;

			});
	
	$(".tree_btns2 .idBtnPicked").live('click', function() {
		var items = $("#sel_prod").find("[type='checkbox']");
		if(items != null){
			var str="";
			var pks="";
			for(var loop=0; loop<items.length; loop++){
				str+=$(items[loop]).next("label").html()+",";
				pks+=$(items[loop]).val().substr(0,20)+",";
			}
			var length1=str.length;
			var length2=pks.length;
			$("#qrygoods").val(str.substring(0, length1-1));
			$("#pk_material").val(pks.substring(0, length2-1));
		}
		
		//$(this).parents(".ui-drop_dialog").slideUp();
		$("#productPicker").slideUp();
		
	});
	
	$(".tree_btns2 .idBtnCancel").live('click', function(){
		$("#prod_filter").val("");
		//$(this).parents(".ui-drop_dialog").slideUp();
		$("#productPicker").slideUp();
		return false;
	})

	this.init = function() {

	}
	
	function len(s) { 
		var l = 0; 
		var a = s.split(""); 
		for (var i=0;i<a.length;i++) { 
			if (a[i].charCodeAt(0)<299) { 
				l++; 
			} else { 
				l+=2; 
			} 
		} 
		return l; 
	}
}

$(function() {
	var goods = new Goods();
	goods.init();
});
